﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using System.Xml;

namespace DE.Sdc.SharePoint.SPContentStructure.Import
{
    [Serializable]
    [XmlType(AnonymousType = true, Namespace = DeploymentConstants.Namespace)]
    [XmlRoot(Namespace = DeploymentConstants.Namespace, IsNullable = false)]
    public class Files : IImportDefinition<SPFolder, SPWeb>
    {
        public Files()
        {
            Items = new List<File>();
        }

        [XmlElement("File")]
        public List<File> Items { get; set; }

        [XmlIgnore]
        public SPFolder UpdatedSPObject { get; internal set; }

        /// <summary>
        /// Adds, modifies or deletes fields for a list
        /// </summary>
        /// <param name="folder">The parent folder where to add, modify or delete the fields</param>
        /// <param name="parentWeb"></param>
        public void ImportDefinition(SPFolder folder, SPWeb parentWeb)
        {
            foreach (File fileDefinition in Items)
            {
                SPFile file = null;
                try
                {
                    file = folder.Files[fileDefinition.Path];
                }
                catch (Exception)
                {
                    // Catch if field does not exists
                }

                fileDefinition.ImportDefinition(file, folder);
            }

            UpdatedSPObject = folder;
        }
    }
}